package com.springboot.study.tests.genericity;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class TaskFeature extends Feature {

    private static final long serialVersionUID = -2434770590153832464L;

    private Dog interestTaskDO;

    private Map<String, Object> featureToMap = new HashMap<>();


    public TaskFeature(Dog interestTaskDO) {
        this.interestTaskDO = interestTaskDO;
        String feature = interestTaskDO.getFeature();

        if (!StringUtils.isEmpty(feature)) {
            this.featureToMap = JSON.parseObject(feature, Map.class);
        }
    }


    private void set(TaskFeatureKey featureKey, Object val) {
        if (null == val) {
            return;
        }
        featureToMap.put(featureKey.code, val);
        interestTaskDO.setFeature(this.toString());
    }


    public Object getFeatureKey(TaskFeatureKey featureKey) {
        return featureToMap.get(featureKey.code);
    }


    @Override
    public String toString() {
        return JSON.toJSONString(featureToMap);
    }

    public Long getInterestAmount() {

        Object o = featureToMap.get(TaskFeatureKey.INTEREST_AMOUNT.code);

        if (null == o) {
            return 0L;
        }

        return Long.valueOf(o.toString());

    }

    public void setInterestAmount(Long interestAmount) {
        set(TaskFeatureKey.INTEREST_AMOUNT, interestAmount);
    }

    public BigDecimal getInterestRate() {
        Object o = featureToMap.get(TaskFeatureKey.INTEREST_RATE.code);

        if (null == o) {
            return BigDecimal.ZERO;
        }

        return new BigDecimal(o.toString());
    }

    public void setInterestRate(BigDecimal interestRate) {
        set(TaskFeatureKey.INTEREST_RATE, interestRate);

    }

    public BigDecimal getInterestWeight() {
        Object o = featureToMap.get(TaskFeatureKey.INTEREST_WEIGHT.code);

        if (null == o) {
            return BigDecimal.ZERO;
        }

        return new BigDecimal(o.toString());
    }

    public void setInterestWeight(BigDecimal interestWeight) {
        set(TaskFeatureKey.INTEREST_WEIGHT, interestWeight);

    }

    public Long getInterestPrice() {
        Object o = featureToMap.get(TaskFeatureKey.INTEREST_PRICE.code);

        if (null == o) {
            return 0L;
        }

        return Long.valueOf(o.toString());
    }

    public void setInterestPrice(Long interestPrice) {
        set(TaskFeatureKey.INTEREST_PRICE, interestPrice);

    }

    /**
     * 回款来源
     *
     * @return
     */
    public Integer getReceiptType() {
        Object o = featureToMap.get(TaskFeatureKey.RECEIPT_TYPE.code);

        if (null == o) {
            return 0;
        }

        return Integer.valueOf(o.toString());
    }

    public void setReceiptType(Integer receiptType) {
        set(TaskFeatureKey.RECEIPT_TYPE, receiptType);

    }

    /**
     * 采购协议回款金额
     *
     * @return
     */
    public Long getReceiptAmount() {
        Object o = featureToMap.get(TaskFeatureKey.RECEIPT_AMOUNT.code);

        if (null == o) {
            return 0L;
        }

        return Long.valueOf(o.toString());
    }


    public void setReceiptAmount(Long receiptAmount) {
        set(TaskFeatureKey.RECEIPT_AMOUNT, receiptAmount);

    }


    // *计息本金（元）	*计息利率（%/天）
    // *计息重量（吨）	*计息价格（元/吨/天）

    public enum TaskFeatureKey {


        INTEREST_AMOUNT("1", "计息本金"),
        INTEREST_RATE("2", "计息利率"),
        INTEREST_WEIGHT("3", "计息重量"),
        INTEREST_PRICE("4", "计息价格"),

        // 回款来源 1:采购订单  2：采购对应的销售订单
        RECEIPT_TYPE("5", "回款来源"),
        RECEIPT_AMOUNT("6", "采购协议回款金额"),
        ;

        public final String code;
        public final String desc;

        TaskFeatureKey(String code, String desc) {
            this.code = code;
            this.desc = desc;
        }


    }
}
